<?php
namespace App\Factory\Refund;

use App\Factory\Wechat\Factory as WechatFactory;
use App\Utils\Other\Safepay\Refund\Mini;
use EasyWeChat\Kernel\HttpClient\HttpClientMethods;
use App\Utils\Other\Payment\Sandpay\Mini as SandpayMini;
use App\Utils\Other\Payment\Neopay\Mini as NeopayMini;
use App\Utils\Other\Payment\Sumpay\Mini as SumpayMini;
use App\Utils\Other\Payment\Chinapnrpay\Mini as ChinapnrpayMini;
use Hyperf\Context\Context;
use function Hyperf\Support\make;


Class Factory {

    /**
     * 衫德微信小程序退款
     * @param array $params
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
//    public function sandpayMini(array $params):array
//    {
//        /** @var Mini $miniPay */
//        $miniPay = make(Mini::class);
//
//        $data = [
//            'orderCode' => $params['out_refund_no'],
//            'oriOrderCode' => $params['out_trade_no'],
//            'refundReason' => $params['reason'],
//            'refundAmount' => str_pad($params['amount'] * 100, 12, '0', STR_PAD_LEFT)
//        ];
//
//        return $miniPay->handle($data);
//    }

    /**
     * 微信退款
     * @param array $params
     * @return array
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \EasyWeChat\Pay\Exceptions\InvalidSignatureException
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
     */
    public function wechatMini(array $params)
    {
        $config = Context::get('pay_config_array');
        /** @var WechatFactory $pay */
        $pay = make(WechatFactory::class);
        $app = $pay->pay($config);

        /** @var HttpClientMethods $client */
        $client = $app->getClient()->withSerialHeader(array_key_first($config['platform_certs']));

        $response = $client->postJson('v3/refund/domestic/refunds', $params);

        try {
            $data = $response->toArray();
            $app->getValidator()->validate($response->toPsrResponse());
        } catch (\Throwable $throwable) {
            logs($response->getContent(false));
            throw $throwable;
        }

        return [
            $params, $data, $data,
            'https://api.mch.weixin.qq.com/v3/refund/domestic/refunds'
        ];
    }

    /**
     * 衫德小程序退款
     * @param array $params
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function sandpayMini(array $params): array
    {
        /** @var SandpayMini $mini */
        $mini = make(SandpayMini::class);
        $mini->setConfig(Context::get('pay_config_array'));
        $params['mid'] = $mini->getConfig('mid');

        return $mini->refund($params);
    }

    /**
     * 衫德小程序退款
     * @param array $params
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function sandpayMiniNotify(array $params, array $config): array
    {
        /** @var SandpayMini $mini */
        $mini = make(SandpayMini::class);
        $mini->setConfig($config);

        return $mini->verify($params);
    }

    /**
     * Safepay小程序退款
     * @param array $params
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function safepayMini(array $params): array
    {
        /** @var Mini $refundFactory */
        $refundFactory = make(Mini::class);
        return $refundFactory->handle($params);
    }

    /**
     * 易生小程序退款
     * @param array $params
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function neopayMini(array $params): array
    {
        /** @var NeopayMini $mini */
        $mini = make(NeopayMini::class);
        $mini->setConfig(Context::get('pay_config_array'));

        return $mini->refund($params);
    }

    /**
     * 统统付小程序退款
     * @param array $params
     * @return array
     */
    public function sumpayMini(array $params): array
    {
        /** @var SumpayMini $mini */
        $mini = make(SumpayMini::class);
        $mini->setConfig(Context::get('pay_config_array'));

        return $mini->refund($params);
    }

    /**
     * 统统付微信小程序支付回调通知
     * @param array $params
     * @param array $config
     * @return array
     */
    public function sumpayMiniNotify(array $params, array $config): array
    {
        /** @var SumpayMini $mini */
        $mini = make(SumpayMini::class);
        $mini->setConfig($config);

        return $mini->verify($params);
    }

    /**
     * 汇付小程序退款
     * @param array $params
     * @return array
     */
    public function chinapnrpayMini(array $params): array
    {
        /** @var ChinapnrpayMini $mini */
        $mini = make(ChinapnrpayMini::class);
        $mini->setConfig(Context::get('pay_config_array'));

        return $mini->refund($params);
    }

    /**
     * 汇付退款回调验证
     * @param array $params
     * @param array $config
     * @return array
     */
    public function chinapnrpayMiniNotify(array $params, array $config): array
    {
        /** @var ChinapnrpayMini $mini */
        $mini = make(ChinapnrpayMini::class);
        $mini->setConfig($config);

        return $mini->refundVerify($params);
    }
}